/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */

#include "DepositManager.h"
#include "Utilities.h"
#include "StockDatabase.h"

namespace QSThink
{

	const QString DepositManager::FileName="deposit.data";
	

	DepositManager& DepositManager::getSingleton()
	{
		static DepositManager inst;
		return inst;
	}

	DepositManager::DepositManager()
	{

		loadDeposits();


	}

	DepositInfo* DepositManager::addDeposit(QDate date,DepositCurrencyType currency,DepositAccountType account,int balance)
	{
		DepositInfo* info=new DepositInfo(date,currency,account,balance);
		_deposits.push_back(info);

		persist();

		return info;
	}

	int DepositManager::getDepositCount()
	{

		return _deposits.size();
	}

	void DepositManager::addListener( DepositListener* listener )
	{
		_listeners.push_back(listener);
	}

	DepositInfo* DepositManager::getDeposit( int index )
	{
		return _deposits[index];//we don't check index, just crash
	}

	
	void DepositManager::persist()
	{
		QFile file(FileName);
		if (!file.open(QFile::WriteOnly)){
			return;
		}

		int cnt=_deposits.size();

		for(int i=0;i<cnt;i++){
			DepositInfo* info=_deposits[i];
			QDate date=info->date();
			int year=date.year();
			int month=date.month();
			int day=date.day();
			
			QString line;
			line+=QString::number(year);
			line+="\t";
			line+=QString::number(month);
			line+="\t";
			line+=QString::number(day);
			line+="\t";
			line+=QString::number(info->currency());
			line+="\t";
			line+=QString::number(_deposits[i]->account());
			line+="\t";
			line+=QString::number(_deposits[i]->balance());
			line+="\n";

			QByteArray bytes= QTextCodec::codecForName("GB2312")->fromUnicode(line);

			file.write(bytes);
		
		}
		
		
	}

	void DepositManager::loadDeposits()
	{
		QFile file(FileName);
		if (!file.open(QFile::ReadOnly)){
			return;
		}


		while (!file.atEnd()) {
			QByteArray bytes = file.readLine();
			if (!bytes.isEmpty()){//GB2312
				QString line=QTextCodec::codecForName("GB2312")->toUnicode(bytes);
				QStringList words=line.split("\t");

				if(words.length()==6){
					int year=words[0].toInt();
					int month=words[1].toInt();
					int day=words[2].toInt();

					QDate date(year,month,day);
					DepositCurrencyType currency=(DepositCurrencyType)words[3].toInt();
					DepositAccountType account=(DepositAccountType)words[4].toInt();
					int balance=words[5].toInt();
					

					DepositInfo* info=new DepositInfo(date,currency,account,balance);
					_deposits.push_back(info);


				}
			}
		}

	}

	void DepositManager::removeDeposit( DepositInfo* info )
	{
		std::vector<DepositInfo*>::iterator it=_deposits.begin();
		for(;it!=_deposits.end();++it){
			if(*it==info){
				//find
				_deposits.erase(it);
				break;
			}

		}
		

		persist();

	}

	float DepositManager::getCapital()
	{
		float total=0;
		int cnt=_deposits.size();
		for(int i=0;i<cnt;++i){
			total+=_deposits[i]->balance();
		}
		return total;
	}

	DepositListener::DepositListener()
	{
		DepositManager::getSingleton().addListener(this);
	}

}
